package com.cskaoyan.service.goods;

import com.cskaoyan.bean.bo.commitbo.CommitBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketComment;
import com.cskaoyan.bean.po.MarketCommentExample;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.util.StringUtil2;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:todo 评论
 * @Author:todo wang xv
 * @Date:todo 2022/11/21
 */
@Service
public class CommentServiceImpl implements CommentService {
    //评论mapper
    @Autowired
    MarketCommentMapper commentMapper;

    /**
     * @Description:todo 显示商品评论
     * @Author:todo wang xv
     * @Date:todo 2022/11/21
     */
    @Override
    public CommonData<MarketComment> queryCommentList(CommitBo info) {
        // 开启分页；
        PageHelper.startPage(info.getPage(), info.getLimit());
        // comment的排序分页查询；
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        // 判空做条件查询
        if (!StringUtil2.isEmpty(info.getUserId())) {
            criteria.andUserIdEqualTo(Integer.parseInt(info.getUserId()));
        }
        if (!StringUtil2.isEmpty(info.getValueId())) {
            criteria.andValueIdEqualTo(Integer.parseInt(info.getValueId()));
        }
        // 排序
        commentExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        List<MarketComment> comments = commentMapper.selectByExample(commentExample);
        PageInfo<MarketComment> pageInfo = new PageInfo<>(comments);
        return CommonData.data(pageInfo);
    }

    /**
    * @Description:todo 删除商品评论
    * @Author:todo wang xv
    * @Date:todo 2022/11/21
    */
    @Override
    public void deleteComment(MarketComment comment) {
        Integer id = comment.getId();
        commentMapper.deleteByPrimaryKey(id);
    }
}
